package com.zdy.springbootrestful.controller;

import com.zdy.springbootrestful.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: Deyou
 * @Date: 2021/4/10
 */
@RestController
public class UserController {
    /***
     * 模拟数据,创建线程安全的Map
     */
    static ConcurrentHashMap<Integer, User> users = new ConcurrentHashMap<>();

    /***
     * 查询所有用户数据
     * @return
     */
    @GetMapping("/users")
    public String getUsers(){
        String s = users.toString();
        return s;
    }

    /***
     * 添加用户功能
     * @param user
     * @return
     */
    @PostMapping("/users")
    public String addUser(@RequestBody User user){
        users.put(user.getId(),user);
        return "添加用户成功！";
    }

    /***
     * 根据Id 查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    public String addUser(@PathVariable("id") Integer id){
        User user = users.get(id);
        return user.toString();
    }

    /***
     * 根据Id 修改用户信息
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/users/{id}")
    public String addUser(@PathVariable("id") Integer id,@RequestBody  User user){
        users.put(id,user);
        return "修改id="+id+"用户成功！";
    }
    @DeleteMapping("/users/{id}")
    public String deleteUser(@PathVariable("id") Integer id){
        users.remove(id);
        return "删除id="+id+"用户数据";
    }


}
